<?php

namespace App\Http\Repository;

use App\PageLabel;

class PageRepository
{
    private $pagelabel;

    public function __construct(PageLabel $pageLabel)
    {
        $this->pagelabel = $pageLabel;
    }

    //得到的模块的信息列表
    public function getPageinfoGroupPagelabel()
    {
        $tmpPageinfo = $this->pagelabel
            ->leftjoin('page_infos', 'page_infos.label', '=', 'page_labels.id')
            ->leftjoin('admins', 'admins.id', '=', 'page_infos.author')
            ->select('page_labels.name as label_name', 'page_labels.id as label', 'page_labels.label as label_label', 'page_infos.author', 'page_infos.name', 'page_infos.id', 'page_infos.updated_at')
            ->orderBy('page_labels.xindex', 'DESC')
            ->orderBy('page_infos.xindex', 'DESC')
            ->get();
        $pageinfo = [];
        foreach ($tmpPageinfo as $info) {
            $pageinfo[$info->label]['name'] = $info->label_name;
            if (!empty($info->id)) {
                $pageinfo[$info->label]['data'][] = $info;
            }
        }

        return $pageinfo;
    }

    //得到可见的企业信息页面,按照企业模块归类
    public function getPageinfoGroupPagelabelCanShow()
    {
        $tmpPageinfo = $this->pagelabel
            ->leftjoin('page_infos', 'page_infos.label', '=', 'page_labels.id')
            ->select('page_infos.*', 'page_labels.name as label_name', 'page_labels.label as label', 'page_infos.created_at as time', 'images.path as image')
            ->leftjoin('images', 'images.id', '=', 'page_infos.image')
            ->orderBy('page_infos.xindex', 'DESC')
            ->where('page_infos.status', 1)
            ->where('page_labels.status', 1)
            ->get()
            ->toArray();
        $pageinfo = [];
        foreach ($tmpPageinfo as $info) {
            if (!empty($info['id'])) {
                $pageinfo[$info['label']][] = $info;
            }
        }

        return $pageinfo;
    }

    //得到企业信息的标签信息
    public function getPageLabel()
    {
        return \App\PageLabel::where('status', 1)
            ->orderBy('xindex')
            ->lists('name', 'label')
            ->toArray();
    }
}